const onPreviewImage = function(e) {
	let current = e.currentTarget.dataset.src;
	let picArr = [];
	picArr.push(current);
	uni.previewImage({
		current: current,
		urls: picArr
	});
};

const onToHomePage = function(e) {
	uni.switchTab({
		url: '/pages/index/index'
	})
}


function objectToQueryString(obj) {
	const queryString = [];
	for (const key in obj) {
		if (obj.hasOwnProperty(key)) {
			queryString.push(`${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`);
		}
	}
	return '?' + queryString.join('&');
}
/**
 * 页面跳转
 * @param url 路径,截取/pages后半段
 * @param params 携带数据，传{k:v,...,k:v}
 * @param type 跳转模式，1跳tab页面，0跳非tab页面
 * @param events 原生接口的events参数，页面间通信接口，用于监听被打开页面发送到当前页面的数据
 */
const p2p = (url, params = null, type = 0, events = {}) => {
	if (type === 1)
		uni.switchTab({
			url: url
		})
	else {
		let str = objectToQueryString(params)
		uni.navigateTo({
			url: '/pages' + url + params ? str : '',
			events: events
		})
	}
}


module.exports = function(obj) {
	obj.onPreviewImage = onPreviewImage;
	obj.onToHomePage = onToHomePage;
	obj.p2p = p2p;
};